<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子水平居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* 第一种水平居中的方式 */
        .box1 {
            width: 300px;
            height: 300px;
            margin: 100px auto;
            border: 5px solid #000;
        }
        .box2 {
            width: 100px;
            height: 100px;
            margin: 0 auto;
            background-color: skyblue;
        }
         /* 第二种水平居中的方式 */
        .box3 {
            position: relative;
            width: 300px;
            height: 300px;
            margin: 0px auto;
            border: 5px solid #f00;
        }
        .box4 {
            position: absolute;
            left: 50%;
            /* margin-left: -50px; */
            transform: translate(-50%);
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
    <div class="box3">
        <div class="box4"></div>
    </div>
</body>
</html>